package com.jc.camerascan.adapter;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.jc.camerascan.R;
import com.jc.camerascan.activity.camera.CameraActivity;
import com.jc.camerascan.activity.mine.UserCenterActivity;
import com.jc.camerascan.core.http.bean.CustomApiResult;
import com.jc.camerascan.core.http.callback.TipCallBack;
import com.jc.camerascan.entity.HomeMenu;
import com.jc.camerascan.entity.mine.GetUserInfoResult;
import com.jc.camerascan.login.LoginActivity;
import com.jc.camerascan.utils.ImageUtil;
import com.jc.camerascan.utils.JurisdictionUtils;
import com.jc.camerascan.utils.MMKVUtils;
import com.jc.camerascan.utils.XToastUtils;
import com.jc.camerascan.widget.GridSpacingItemDecoration;
import com.xuexiang.xhttp2.XHttp;
import com.xuexiang.xhttp2.callback.CallBackProxy;
import com.xuexiang.xhttp2.exception.ApiException;
import com.xuexiang.xui.utils.DensityUtils;
import com.xuexiang.xui.widget.dialog.materialdialog.DialogAction;
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog;
import com.xuexiang.xui.widget.imageview.RadiusImageView;

/**
 * @ProjectName: PhotoRecognition
 * @Package: com.jc.camerascan.adapter
 * @ClassName: MyMenuListAdapter
 * @Description: 首页菜单适配器
 * @Author: XuChi
 * @CreateDate: 2020/7/14 16:09
 * @Version: 1.0
 */
public class MyMenuListAdapter extends BaseQuickAdapter<HomeMenu, BaseViewHolder> {

    public MyMenuListAdapter() {
        super(R.layout.item_menulist);
    }

    @Override
    protected void convert(BaseViewHolder helper, HomeMenu item) {
        helper.setText(R.id.title, item.getTitle());
        RecyclerView list = helper.getView(R.id.list);

        GridLayoutManager layoutManager = new GridLayoutManager(getContext(), 4);
        //配置布局
        list.setLayoutManager(layoutManager);
        list.addItemDecoration(new GridSpacingItemDecoration(4, DensityUtils.dp2px(getContext(), 5), false));
        list.setNestedScrollingEnabled(false);
        list.setHasFixedSize(false);
        MyMenuItemListAdapter myMenuItemListAdapter = new MyMenuItemListAdapter();
        myMenuItemListAdapter.setNewData(item.getData());
        myMenuItemListAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (!JurisdictionUtils.isLogin()) {
                    new MaterialDialog.Builder(getContext())
                            .title("去登录")
                            .content("请先登录，然后再进行操作。")
                            .positiveText("确定")
                            .negativeText("取消")
                            .onPositive(new MaterialDialog.SingleButtonCallback() {
                                @Override
                                public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                    LoginActivity.start(getContext());
                                }
                            })
                            .show();
                    return;
                }
                if (!JurisdictionUtils.isVip()) {
                    getUserInfo(myMenuItemListAdapter.getItem(position).getUrl(), myMenuItemListAdapter.getItem(position).getTitle());
                } else {
                    String type = myMenuItemListAdapter.getItem(position).getUrl();
                    if (myMenuItemListAdapter.getItem(position).getTitle().contains("车牌识别")) {
                        type = "CARPSB";
                    }
                    CameraActivity.open(getContext(), type);
                }
            }
        });
        list.setAdapter(myMenuItemListAdapter);
    }

    private void getUserInfo(final String type, final String title) {
        XHttp.get("/api/User/GetInfo")
                .execute(new CallBackProxy<CustomApiResult<GetUserInfoResult>, GetUserInfoResult>(new TipCallBack<GetUserInfoResult>() {
                    @Override
                    public void onSuccess(GetUserInfoResult response) throws Throwable {
                        String typeString = type;
                        MMKVUtils.put("is_vip", response.getIs_vip());
                        if (response.isIsFreeUse()) {
                            if (title.contains("车牌识别")) {
                                typeString = "CARPSB";
                            }
                            CameraActivity.open(getContext(), typeString);
                        } else {
                            if (response.getNotFreeUseErrorMsg() != null)
                            {
                                XToastUtils.warning(response.getNotFreeUseErrorMsg());
                            }else {
                                XToastUtils.warning("当日免费次数已用完");
                            }
                            new MaterialDialog.Builder(getContext())
                                    .title("购买会员")
                                    .content("您还未购买会员，请先购买会员。")
                                    .positiveText("确定")
                                    .negativeText("取消")
                                    .onPositive(new MaterialDialog.SingleButtonCallback() {
                                        @Override
                                        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                            UserCenterActivity.start(getContext());
                                        }
                                    })
                                    .show();
                        }
                    }

                    @Override
                    public void onError(ApiException e) {
                        super.onError(e);
                        new MaterialDialog.Builder(getContext())
                                .title("购买会员")
                                .content("您还未购买会员，请先购买会员。")
                                .positiveText("确定")
                                .negativeText("取消")
                                .onPositive(new MaterialDialog.SingleButtonCallback() {
                                    @Override
                                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                        UserCenterActivity.start(getContext());
                                    }
                                })
                                .show();
                    }
                }) {
                });
    }
}
